<template>
  <div class="register">
    <div class="register-container">
      <div class="left-section">
        <div class="animation-container">
          <lottie-player
              src="https://lottie.host/b00aba23-4316-427f-814e-518d0f27e815/IJ9gf6Yy3M.json"
              background="transparent"
              speed="1"
              style="width: 100%; height: 450px;"
              loop
              autoplay
          ></lottie-player>
        </div>
        <h2 class="slogan">快捷 · 舒适 · 安全</h2>
        <p class="sub-slogan">开启您的智慧出行之旅</p>
      </div>
      <div class="right-section">
        <div class="register-card">
          <h1 class="register-title">行易达</h1>
          <p class="register-subtitle">铁路出行，轻松便捷</p>
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
            <el-form-item prop="username">
              <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="请输入账号">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                auto-complete="off"
                placeholder="请输入密码"
                @keyup.enter.native="handleRegister"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                auto-complete="off"
                placeholder="请确认密码"
                @keyup.enter.native="handleRegister"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="registerForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                style="width: 63%"
                @keyup.enter.native="handleRegister"
              >
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="register-code">
                <img :src="codeUrl" @click="getCode" class="register-code-img"/>
              </div>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleRegister"
              >
                <span v-if="!loading">注 册</span>
                <span v-else>注 册 中...</span>
              </el-button>
              <div class="register-options">
                <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="el-register-footer">
      <span>Copyright © 2024 行易达 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
    const script = document.createElement('script');
    script.src = "https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js";
    document.head.appendChild(script);
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.register {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff, #69c0ff);
}

.register-container {
  display: flex;
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.left-section {
  flex: 1.4;
  background-color: #5CB6FF;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.animation-container {
  width: 100%;
  max-width: 600px;
}

.slogan {
  font-size: 36px;
  font-weight: bold;
  color: #fff;
  margin-top: 20px;
  text-align: center;
}

.sub-slogan {
  font-size: 18px;
  color: #fff;
  margin-top: 10px;
  text-align: center;
}

.right-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.register-card {
  width: 100%;
  max-width: 400px;
}

.register-title {
  font-size: 36px;
  font-weight: bold;
  color: #1890ff;
  text-align: center;
  margin-bottom: 12px;
}

.register-subtitle {
  font-size: 18px;
  color: #8c8c8c;
  text-align: center;
  margin-bottom: 40px;
}

.register-form {
  :deep(.el-input) {
    height: 42px;
    
    input {
      height: 42px;
      padding-left: 15px;
      border-radius: 4px;
      background-color: #f5f7fa;
      border: 1px solid #e4e7ed;
      
      &:focus {
        border-color: #1890ff;
      }
    }
  }

  :deep(.input-icon) {
    height: 42px;
    width: 14px;
    margin-left: 2px;
    color: #909399;
  }

  :deep(.el-button--primary) {
    background: #1890ff;
    border-color: #1890ff;
    height: 42px;
    font-size: 16px;
    
    &:hover {
      background: #40a9ff;
      border-color: #40a9ff;
    }
  }
}

.register-code {
  width: 33%;
  height: 42px;
  float: right;
  
  img {
    height: 42px;
    cursor: pointer;
    border-radius: 4px;
  }
}

.register-options {
  margin-top: 20px;
  text-align: right;
  
  .link-type {
    color: #1890ff;
    text-decoration: none;
    font-size: 14px;
    
    &:hover {
      color: #40a9ff;
    }
  }
}

.el-register-footer {
  margin-top: 20px;
  color: #fff;
  font-size: 12px;
}
</style>
